const app = getApp()

import axios from '../../utils/axios'

Page({

  data: {
    sculpture: 'https://image.petintech.com/images/loginImg.png',
    nickName: '请点击登录',
    userInfo: null,
    couponCount: 0,
    petsCount: 0,
    ordersCount: 0
  },

  onLoad: function (options) {
    if (app.globalData.userInfo) {
      this.setData({
        nickName: app.globalData.userInfo.nickname,
        sculpture: app.globalData.userInfo.sculpture,
        userInfo: app.globalData.userInfo
      })
    }

  },

  onShow: function () {

    this.initInfo()

    this.getTabBar().setData({
      selected: 3
    })
  },

  // 初始化加载
  initInfo() {

    axios("/user/personal").then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        this.setData({
          couponCount: res.data.result.coupons,
          petsCount: res.data.result.pets,
          ordersCount: res.data.result.orders
        })
      } else {
        wx.showToast({
          title: '请登录后再进行操作！',
          icon: 'none',
          duration: 1500
        })
      }
    })

  },

  // 用户点击登录
  getUserInfo: function (e) {
    if (e.detail.userInfo) {
      const self = this
      app.signin(e.detail, function (res) {
        console.log(res)
        self.setData({
          userInfo: res,
          nickName: res.nickname,
          sculpture: res.sculpture
        })
      })
    }

  },

  // 点击订单
  checkOrder: function (e) {
    if (this.isLogin()) {

    } else {
      wx.navigateTo({
        url: '/pages/orderInfo/orderInfo?index=' + e.currentTarget.dataset.current,
      })
    }

  },

  // 点击宠物登记
  addPet: function () {
    if (this.isLogin()) {

    } else {
      wx.navigateTo({
        url: `/pages/petsLis/petsLis?store_id=${app.globalData.storeId}`
      })
    }
  },

  // 点击申请团长
  group: function () {
    wx.navigateTo({
      url: '/pages/group/recruit/recruit',
    })
  },

  // 点击我的优惠券
  checkCoupon: function () {
    if (this.isLogin()) {

    } else {
      wx.navigateTo({
        url: '/pages/uifoncoupon/uifoncoupon?show=not',
      })
    }

  },

  // 判断是否已登录
  isLogin: function () {
    if (app.globalData.userToken == '') {
      wx.showToast({
        title: '请先登录哦！',
        icon: 'none',
        duration: 1300
      })
      return true
    } else {
      return false
    }
  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})